package com.itcast.demo_one

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class PageOneActivity : AppCompatActivity() {

    // 定义一个常量
    private val TAG = "itCast_A页面"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        Log.d(TAG, "onCreate")

        enableEdgeToEdge()
        setContentView(R.layout.activity_page_one)

        // 1. 实现从页面一跳转到页面二
        findViewById<View>(R.id.btn_action_next).setOnClickListener {
            // 1.1 创建一个Intent对象
            val intent = Intent(this, PageTwoActivity::class.java)
            // 1.2 携带参数
            intent.putExtra("name", "张三")
            intent.putExtra("age", 18)
            intent.putExtra("height", 1.8f)
            // 1.3 实现页面跳转
            startActivity(intent)
        }

        // 使用新版 Activity Result API（推荐）
        // 注册一个Activity结果监听器，用于处理从PageTwoActivity返回的结果
        // 当PageTwoActivity返回RESULT_OK时，获取名为"result_data"的字符串数据
        val resultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
            if (result.resultCode == RESULT_OK) {
                val data = result.data?.getStringExtra("result_data")
                Toast.makeText(this, data, Toast.LENGTH_SHORT).show()
            }
        }

        // 2. 实现从页面一跳转到页面二
        findViewById<View>(R.id.button8).setOnClickListener {
            val intent = Intent(this, PageTwoActivity::class.java)
            // 1.2 携带参数
            intent.putExtra("name", "张三")
            intent.putExtra("age", 18)
            intent.putExtra("height", 1.8f)
            resultLauncher.launch(intent)
        }

    }

    override fun onStart() {
        super.onStart()
        Log.d(TAG, "onStart")
    }

    override fun onResume() {
        super.onResume()
        Log.d(TAG, "onResume")
    }

    override fun onPause() {
        super.onPause()
        Log.d(TAG, "onPause")
    }

    override fun onStop() {
        super.onStop()
        Log.d(TAG, "onStop")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d(TAG, "onDestroy")
    }

    override fun onRestart() {
        super.onRestart()
        Log.d(TAG, "onRestart")
    }
}